package Hander;

import View.*;
import entity.AdminDo;
import service.AdminService;
import service.AdminServiceImpl;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;


public class LonginViewHander extends KeyAdapter implements ActionListener {
    private LoginView loginView;
    public LonginViewHander(LoginView longinView) {
        this.loginView = longinView;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton =(JButton) e.getSource();
        String text=jButton.getText();
        System.out.println(text);
        if("重置".equals(text)){
            System.out.println("开始重置！！！");
            loginView.getUserText().setText("");
            loginView.getPasswordField().setText("");
            System.out.println("重置成功！！！");
        } else if ("登录".equals(text)) {
            login();
        }else if ("注册".equals(text)) {
            enroll();
        }
    }

    private void login() {
        System.out.println("开始登陆！！！");
        System.out.println("登录");
        String usename=loginView.getUserText().getText();//得到输入的用户名
        char[] password=loginView.getPasswordField().getPassword();//获得用输入的密码
        String pwd=new String(password);////获得用输入的密码
        //输出一下获得账户密码
        System.out.println(usename);
        System.out.println(pwd);
        if(usename==null||"".equals(usename.trim())){
            JOptionPane.showMessageDialog(loginView,"用户名为空！");
            return;
        }
        if(pwd==null||"".equals(pwd.trim())){
            JOptionPane.showMessageDialog(loginView,"密码为空！");
            return;
        }
        AdminService adminService = new AdminServiceImpl();
        //创建一个登录对象
        AdminDo adminDo=new AdminDo();
        //使用输入的信息创建一个登录对象。
        adminDo.setUserName(usename);//usename是用户输入
        adminDo.setPwd(pwd);//pwd是用户输入
        //查询这个登录对象，看看是否可以查询成功！！
        String reselt=adminService.validateAdmin(adminDo);
        if (reselt.equals("ProjectLeader")){
            LeaderView leaderView = new LeaderView("项目负责人");
            leaderView.setRole("ProjectLeader");
            leaderView.addBtn.setVisible(false);//
            leaderView.updataBtn.setVisible(false);
            leaderView.deteleBtn.setVisible(false);

            loginView.dispose();
        }else if(reselt.equals("DepartmentHead")){
            LeaderView leaderView = new LeaderView("部门负责人");
            leaderView.setRole("DepartmentHead");
            loginView.dispose();
        }else if(reselt.equals("Personnel")){
            LeaderView leaderView = new LeaderView("普通职员");
            leaderView.setRole("Personnel");
            leaderView.addBtn.setVisible(false);//
            leaderView.updataBtn.setVisible(false);
            leaderView.deteleBtn.setVisible(false);
            loginView.dispose();
        }else if(reselt.equals("数据库连接失败！")){
            JOptionPane.showMessageDialog(loginView,"数据库连接失败！");
        } else {
            JOptionPane.showMessageDialog(loginView,"用户名密码错误！");
        }

    }

    private void enroll() {
        new EnrollView();
    }
}
